package com.ts.log;

import com.ts.comm.SysClient;
import com.ts.login.bean.UserInfo;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * 通用日志
 * Created by xhc on 2017/3/30.
 */
@Component
@Aspect
public class AppLogAspect {

    Logger log=Logger.getLogger(AppLogAspect.class);

    @Resource
    private LogDao logDao;

    //配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
    @Pointcut("execution(* com.ts..*.*(..))")
    public void anyMethod(){

    }

    /*@Before("anyMethod()")
    public void A(JoinPoint point){
        String sMethodName=point.getSignature().getName();
        log.info("before:["+sMethodName+"]");
    }*/

    @After("anyMethod()")
    public void B(JoinPoint point){

        Signature sig=point.getSignature();
        //判断是否是方法
        try {
            if(sig instanceof MethodSignature)
            {
                MethodSignature msig = (MethodSignature) sig;
                Class[] parameterTypes = msig.getMethod().getParameterTypes();
                //获取截取的方法名称
                String sMethodName=point.getSignature().getName();
                // 获得被拦截的方法
                Method method = null;
                try {
                    method=point.getTarget().getClass().getMethod(sMethodName,parameterTypes);
                } catch (NoSuchMethodException e) {

                }
                if(null!=method){
                    // 判断是否包含自定义的注解，AppLog
                    if (method.isAnnotationPresent(AppLog.class)) {

                        HttpServletRequest request= ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                        HttpSession session = request.getSession();
                        UserInfo u= (UserInfo) session.getAttribute("user");

                        AppLog appLog = method.getAnnotation(AppLog.class);
                        String sModelCode=appLog.mCode();
                        String sDesc=appLog.desc();

                        HashMap<String,Object> params=new HashMap<String,Object>();
                        params.put("fModelCode",sModelCode);
                        params.put("fOpDesc",sDesc);
                        params.put("fIP", SysClient.getIpAddr(request));
                        try {
                            params.put("fUserCode",u.getUserCode());
                        } catch (Exception e) {
                            params.put("fUserCode","");
                            log.error("记录日志：用户代码为空");
                        }
                        logDao.addLog(params);
                    }
                }

            }
        } catch (SecurityException e) {
            log.error(e.getLocalizedMessage());
        }

    }

    /*@Around("anyMethod()")
    public Object C(ProceedingJoinPoint  point) throws Throwable{

        String sMethodName=point.getSignature().getName();
        log.info("Around:["+sMethodName+"]");
        return point.proceed();
    }*/
}
